<?php

class Authorization_IndexController
        extends Zend_Controller_Action {

    private $authorization_service;

    public function init() {
        $this->authorization_service = new Authorization_Service();
    }

    /**
     * Обслуживание попыток входа в систему
     */
    public function indexAction() {
        $form = new Authorization_Form_Authorization();
        if (!$this->_request->isPost()) {
            $this->view->form = $form;
        }
        else {
            if (!$form->isValid($_POST)) {
                $this->view->form = $form;
                $this->view->message = Authorization_Service::DATA_FAIL;
            }
            else {
                $username = $this->_request->getPost('Login');
                $password = $this->_request->getPost('Password');
                if ($this->authorization_service->login($username, $password)) {
                    $member = Zend_Auth::getInstance()->getIdentity();
                    return $this->_redirect($member['Role']);
                }
                else {
                    $this->view->form = $form;
                    $this->view->message =
                            Authorization_Service::LOGIN_DATA_FAIL;
                }
            }
        }
    }

    /**
     * Обслуживание попыток выхода из системы
     */
    public function logoutAction() {
        $this->authorization_service->logout();
        $this->_redirect('/');
    }

}